package zrar;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import zrar.panel.ActionTouchImpl;
import zrar.util.ExcelUtil;
import zrar.util.SystemUtil;
import zrar.util.PropertiesUtil;

//基本设置 界面
public class BaseSetFrame extends Observable implements ActionTouchImpl {

	private static final long serialVersionUID = 1L;
	private static final int CHANGE=5;
	private int actionNum;
	
	private JFrame frame;
	
	/**top**/
	private JPanel topPane;
	private JLabel beij_lb;//背景
	private JTextField img_txt;
	private JButton img_but;
	private JFileChooser imgChooser;
	private JFileChooser saveChooser;
	
	private JLabel speed_lb;//滚动速度
	private JTextField speed_txt;
	
	private JLabel result_lb;//结果保存路径
	private JTextField result_txt;
	
	private JLabel save_lb;//抽奖时程序会自动保存结果,如有意外未保存，则需手动保存中奖信息
	private JButton result_but; //保存中奖结果
	
	private JLabel scroll_lb;//结果显示速度
	private JLabel scroll_tis;//结果显示速度  提示
	private JTextField scroll_txt;
	/**top**/
	
	/**bottom**/
	private JPanel bottomPane;
	private JButton save_but;
	private JButton cancel_but;
	/**bottom**/
	
	static final int WIDTH = 480;
	static final int HEIGHT = 350;
	private int position_x=30;	
	private int position_y=20;
	
	public BaseSetFrame(){
		frame = new JFrame();
		frame.setTitle("基本设置");
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//顶部
		topPane = new JPanel();	
		topPane.setLayout(null);
		
		beij_lb = new JLabel("背景图片");
		img_txt = new JTextField(15);		
		img_but = new JButton("浏览...");
		
		result_lb = new JLabel("结果保存路径");
		result_txt = new JTextField(15);
		result_txt.setText("D:\\");
		
		imgChooser = new JFileChooser("D:\\");
		
		speed_lb = new JLabel("抽奖滚动速度");
		speed_txt =  new JTextField(15);
		
		result_but = new JButton("结果另存");
		saveChooser = new JFileChooser("D:\\");
		File txtfile = new File(saveChooser.getCurrentDirectory().getAbsolutePath()+"/result.txt");
		saveChooser.setSelectedFile(txtfile);
		
		save_lb = new JLabel("**如果程序未自动保存中奖结果");
		save_lb.setForeground(Color.red);
		
		scroll_lb = new JLabel("结果显示速度");
		scroll_txt =  new JTextField(15);
		scroll_tis = new JLabel("如果数字为0 ,则不滚动");
		
		topPane.add(beij_lb);
		topPane.add(img_txt);
		topPane.add(img_but);
		topPane.add(result_lb);
		topPane.add(result_txt);
		topPane.add(speed_lb);
		topPane.add(speed_txt);
		topPane.add(result_but);
		topPane.add(save_lb);
		
		topPane.add(scroll_lb);
		topPane.add(scroll_txt);
		topPane.add(scroll_tis);
		
		//底部
		bottomPane = new JPanel();
		FlowLayout flow = new FlowLayout(FlowLayout.CENTER,60,5);
		bottomPane.setLayout(flow);
		save_but = new JButton("保存");
		cancel_but = new JButton("取消");
		
		bottomPane.add(save_but);
		bottomPane.add(cancel_but);
		
		init();
		frame.add(topPane);
		frame.add(bottomPane);
		frame.setSize(WIDTH, HEIGHT);
		SystemUtil.showInCenter(frame);
	}
	
	private void init(){
		frame.setLayout(null);
		frame.getContentPane().setBackground(Color.WHITE);
		
		topPane.setBounds(position_x,position_y, 400,210 );
		bottomPane.setBounds(position_x,position_y+230, 400,40 );
		
		beij_lb.setBounds(15,15, 100,25);
		img_txt.setBounds(105,15, 170,25);
		img_but.setBounds(300,15, 70,25);
				
		speed_lb.setBounds(15,55, 100,25);
		speed_txt.setBounds(105,55, 170,25);
		
		result_lb.setBounds(15,95, 100,25);
		result_txt.setBounds(105,95, 170,25);
		
		result_but.setBounds(15,135, 110,25);
		save_lb.setBounds(135,135, 280,25);
		
		scroll_lb.setBounds(15,175, 100,25);
		scroll_txt.setBounds(105,175, 80,25);
		scroll_tis.setBounds(210,175, 150,25);
		
		img_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				imgFile_Action(e);
			}
		});
		save_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				save_Action(e);	
			}
		});
		cancel_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				cancel_Action(e);
			}
		});
		
		result_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				result_Action(e);
			}
		});
	}
	
	//结果另存
	protected void result_Action(MouseEvent e) {		
		saveChooser.setDialogTitle("请选择 txt文件或Excle(xls文件)");
		String path = SystemUtil.result_path;
		//System.out.println("path:"+path);
		try {
			if(path!=null&&!path.trim().isEmpty()){			
				saveChooser.setCurrentDirectory(new File(path));
				//System.out.println("set path");
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		int result = saveChooser.showSaveDialog(null);
		
		File file = null;
		//当用户没有选择文件,而是自己键入文件名称时,系统会自动以此文件名建立新文件.
		if (result == JFileChooser.APPROVE_OPTION) {
			
			file = saveChooser.getSelectedFile();			
			try {
				if(SystemUtil.checkFileExtendName(file.getName(), "txt")){
					ExcelUtil.writeTXT(file);
				}else if(SystemUtil.checkFileExtendName(file.getName(), "xls")){				
					ExcelUtil.writeExcel(file);				
				}else{
					JOptionPane.showMessageDialog(null, "请选择 txt文件或xls文件");
					return;
				}
			} catch (Exception e1) {
				
				JOptionPane.showMessageDialog(null, "导出失败:"+e1);
				e1.printStackTrace();
				return;
			}
			
			
		}
	}

	public void hideFrame(){
		frame.setVisible(false);
	}
	
	public void showFrame(){
		initData();
		frame.setVisible(true);
	}
	
	public void initData(){
		Map<String,String> propsMap = PropertiesUtil.readProperties("award.properties");
		img_txt.setText(propsMap.get("背景图片"));
		result_txt.setText(propsMap.get("保存路径"));
		speed_txt.setText(propsMap.get("滚动速度"));
		scroll_txt.setText(propsMap.get("结果显示速度"));
	}
	
	protected void cancel_Action(MouseEvent e) {
		hideFrame();
	}

	protected void save_Action(MouseEvent e) {
		
		String img = img_txt.getText();
		String path = result_txt.getText();
		String speed = speed_txt.getText();
		String scroll = scroll_txt.getText();
		
		if(!checkInputData(img,path,speed,scroll)){									
			return;
		}
		
		
		//写入 配置文件数据
		Map<String,String> saveMap = new HashMap<String,String>();
		saveMap.put("背景图片", img);
		saveMap.put("保存路径", path);
		saveMap.put("滚动速度",speed);
		saveMap.put("结果显示速度",scroll);
		PropertiesUtil.writeProperties("award.properties", saveMap);
		
		//改变SystemUtil中的变量
		SystemUtil.backgroud_image=img;
		SystemUtil.result_path=path;
		SystemUtil.speed = Integer.parseInt(speed);
		SystemUtil.scroll = Integer.parseInt(scroll);
		
		//改变背景
		notifyTheObservers(CHANGE);
		
		hideFrame();
	}


	private boolean checkInputData(String img, String path, String speed,String scroll) {
		
		if (path == null || path.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "保存路径不能为空");
			return false;
		}
		boolean istxt = SystemUtil.checkFileExtendName(path, "txt");
		boolean isxls = SystemUtil.checkFileExtendName(path, "xls");
		if(istxt||isxls){
			
		}else{
			JOptionPane.showMessageDialog(null, "保存路径的文件格式必须是txt或xls");
			return false;
		}
		if (!SystemUtil.isInteger(speed)) {
			JOptionPane.showMessageDialog(null, "抽奖滚动速度不是整数");
			return false;
		}
		if(Integer.parseInt(speed)==0){
			JOptionPane.showMessageDialog(null, "抽奖滚动速度应大于0");
			return false;
		}
		
		if (!SystemUtil.isInteger(scroll)) {
			JOptionPane.showMessageDialog(null, "结果显示速度不是整数");
			return false;
		}
		return true;
	}

	protected void imgFile_Action(MouseEvent e) {
		imgChooser.setApproveButtonText("确定");
		imgChooser.setDialogTitle("打开文件");
		File file = null;
		int result = imgChooser.showOpenDialog(frame);

		if (result == JFileChooser.APPROVE_OPTION) {
			file = imgChooser.getSelectedFile();
			
			boolean isJPG=false;
			boolean isPNG=false;
						
			isJPG=SystemUtil.checkFileExtendName(file.getName(), "jpg");
			isPNG=SystemUtil.checkFileExtendName(file.getName(), "png");
			
			if(isJPG||isPNG){
				img_txt.setText(file.getPath());
			}else{
				JOptionPane.showMessageDialog(null, "请选择jpg或png图片文件");
			}
		}
	}
	
	

	public static void main(String[] args) {
		BaseSetFrame baseSetFrame=new BaseSetFrame();
		baseSetFrame.showFrame();
	}
	
	private void notifyTheObservers(int flag){		
		setActionNum(flag);
		this.setChanged();
	    this.notifyObservers();
	}

	@Override
	public int getActionNum() {		
		return actionNum;
	}

	@Override
	public void setActionNum(int actionNum) {
		this.actionNum = actionNum;		
	}

}
